package com.wanghui.core.module.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.hsqldb.Server;
import org.hsqldb.persist.HsqlProperties;
import org.hsqldb.server.ServerAcl.AclFormatException;
import org.hsqldb.util.DatabaseManager;
import org.junit.Test;

/**
 * HSQL DB工具类,方便测试时启动DB服务 HSQL服务器配置信息通过资源文件获取
 * 
 * @author Xredman Oct 17, 2011
 */
public class HsqlServerUtils {

	public static void startNormalDBServer() throws IOException,
			AclFormatException {
		Server dbServer = new Server();
		Properties props = new Properties();
		InputStream ins = ClassLoader
				.getSystemResourceAsStream("HsqlServer.properties");
		props.load(ins);
		ins.close();
		dbServer.setProperties(new HsqlProperties(props));
		dbServer.start();
		System.out.println("在Console中敲入回车,以停止Hsql DB服务.");
		if (System.in.read() != 0) {
			dbServer.stop();
			System.out.println("Hsql DB stopped");
		}
	}
	
	public static void startNormalDBGUIQuery() throws IOException {
		List<String> args = new ArrayList<String>();
		Properties props = new Properties();
		InputStream ins = ClassLoader
				.getSystemResourceAsStream("HsqlServer.properties");
		props.load(ins);
		// Driver
		/*args.add("-driver");
		args.add("org.hsqldb.jdbcDriver");*/
		// url
		/*args.add("-url");
		args.add("jdbc:hsqldb:hsql://localhost:"
				+ props.getProperty("server.port", "9001") + "/"
				+ props.getProperty("server.dbname.0", "hsql"));*/
		
		// User Name
		//args.add("-user");
		//args.add(props.getProperty("username", "SA"));
		//args.add("-password");
		//args.add(props.getProperty("password", ""));
		
		DatabaseManager.main((String[])args.toArray(new String[args.size()]));
	}
}
